package com.lekang.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.mapper.ElderlyMapper;
import com.lekang.domain.Elderly;
import com.lekang.service.IElderlyService;

/**
 * 老人Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-13
 */
@Service
public class ElderlyServiceImpl implements IElderlyService 
{
    @Autowired
    private ElderlyMapper elderlyMapper;

    /**
     * 查询老人
     * 
     * @param id 老人主键
     * @return 老人
     */
    @Override
    public Elderly selectElderlyById(Long id)
    {
        return elderlyMapper.selectElderlyById(id);
    }
//入退专用查询老人
    @Override
    public Elderly selectElderlyById1(Long id) {
        return elderlyMapper.selectElderlyById1(id);
    }

    /**
     * 查询老人列表
     * 
     * @param elderly 老人
     * @return 老人
     */
    @Override
    public List<Elderly> selectElderlyList(Elderly elderly)
    {
        List<Elderly> elderlyList =elderlyMapper.selectElderlyList(elderly);

        for(Elderly elderly1:elderlyList){
            String nameStr=elderly1.getCaregiverNameStr();
            List<String> nameList=new ArrayList<>();
            if(nameStr!=null){
                String[] names=nameStr.split(",");
                for(String name:names){
                    if(!name.isEmpty()){
                        nameList.add(name);
                    }
                }
            }
            elderly1.setCaregiverName(nameList);
        }
        return elderlyList;
    }

    /**
     * 新增老人
     * 
     * @param elderly 老人
     * @return 结果
     */
    @Override
    public int insertElderly(Elderly elderly)
    {
        return elderlyMapper.insertElderly(elderly);
    }

    /**
     * 修改老人
     * 
     * @param elderly 老人
     * @return 结果
     */
    @Override
    public int updateElderly(Elderly elderly)
    {
        return elderlyMapper.updateElderly(elderly);
    }

    /**
     * 批量删除老人
     * 
     * @param ids 需要删除的老人主键
     * @return 结果
     */
    @Override
    public int deleteElderlyByIds(Long[] ids)
    {
        return elderlyMapper.deleteElderlyByIds(ids);
    }

    /**
     * 删除老人信息
     * 
     * @param id 老人主键
     * @return 结果
     */
    @Override
    public int deleteElderlyById(Long id)
    {
        return elderlyMapper.deleteElderlyById(id);
    }

    /**
     * 解除老人与床位的绑定关系
     * 
     * @param elderlyId 老人ID
     * @return 结果
     */
    @Override
    public int unbindBedFromElderly(Long elderlyId)
    {
        return elderlyMapper.unbindBedFromElderly(elderlyId);
    }
}
